package com.calendar.day;

import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

public class DayView extends Activity {

	private static final int HOUR_LABEL_WIDTH = 50;
	private static final int HOUR_LABEL_HEIGHT = 100;
	private static final int HOUR_LABEL_COLOR = Color.GRAY;

	
	private static final int EVENT_COLOR = Color.GREEN;
	
	private CharSequence times[] = {
			
		"12 am",
		"1 am",
		"2 am",
		"3 am",
		"4 am",
		"5 am",
		"6 am",
		"7 am",
		"8 am",
		"9 am",
		"10 am",
		"11 am",
		"12 pm",
		"1 pm",
		"2 pm",
		"3 pm",
		"4 pm",
		"5 pm",
		"6 pm",
		"7 pm",
		"8 pm",
		"9 pm",
		"10 pm",
		"11 pm",
	};
	
	private Event events[] = {
	
		new Event( "Event 1", 1.0, 0.5 ),
		new Event( "Event 2", 3.0, 0.75 ),
		new Event( "Event 3", 1.25, 0.5 ),
//		new Event( "Event 4", 1.5, 0.5 ),
//		new Event( "Event 5", 0.75, 1.75 ),
	};

	/** Called when the activity is first created. */
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate( Bundle savedInstanceState ) {
    	    	
        super.onCreate( savedInstanceState );
        
        ScrollView scroll = new ScrollView( this );
        
        AbsoluteLayout components = new AbsoluteLayout( this );
        components.setBackgroundColor( Color.WHITE );
                
        //	Add the hour labels.
        for( int i = 0; i < times.length; i++ ) {
        	
        	TextView time = new TextView( this );
        	time.setText( times[i] );
        	time.setGravity( Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL );
        	time.setBackgroundColor( HOUR_LABEL_COLOR );
        	components.addView( time, new AbsoluteLayout.LayoutParams( HOUR_LABEL_WIDTH,
        															   HOUR_LABEL_HEIGHT,
        															   0,
        															   HOUR_LABEL_HEIGHT * i ) );
        }
        
        //	Draw a line between the labels and the events.
        TextView verticalLine = new TextView( this );
        verticalLine.setBackgroundColor( Color.BLACK );
        components.addView( verticalLine, new AbsoluteLayout.LayoutParams( 1,
        																   times.length * HOUR_LABEL_HEIGHT,
        																   HOUR_LABEL_WIDTH,
        																   0 ) );
        
        //	Add lines to break everything up.
        for( int i = 1; i < times.length; i++ ) {
        	
        	Display display = ( (WindowManager)getSystemService( Context.WINDOW_SERVICE ) ).getDefaultDisplay();
        	
        	TextView line = new TextView( this );
        	line.setBackgroundColor( Color.BLACK );
        	components.addView( line, new AbsoluteLayout.LayoutParams( display.getWidth(),
        															   1,
        															   0,
        															   HOUR_LABEL_HEIGHT * i ) );
        }
        
        //	Detect concurrent events.
        for( int i = 0; i < events.length; i++ ) {
        	
        	//	Find conflicts with previously process events.
        	for( int j = 0; j < i; j++ ) {
        		
        		double e0Start = events[i].startTime;
        		double e0End = events[i].startTime + events[j].length;
        		double e1Start = events[j].startTime;
        		double e1End = events[j].startTime + events[j].length;
          		
        		if( ( ( e0Start < e1Start ) && ( e1Start < e0End ) ) ||
        			( ( e1Start < e0Start ) && ( e0Start < e1End ) ) ) {
        			
        			//	Events overlap.
        			if( events[j].concurrentEvents == null ) {
        				
        				//	This is the first time the other event has been in a conflict.
        				events[j].concurrentEvents = new Vector<Event>();
        				events[j].concurrentEvents.add( events[j] );
        			}
        			
        			//	Add this event to the list of conflicts.
        			events[i].concurrentPos = events[j].concurrentEvents.size();
        			events[i].concurrentEvents = events[j].concurrentEvents;
        			events[i].concurrentEvents.add( events[i] );
        			
        			break;
				}
        	}
        }
        
        //	Add the events.
        for( int i = 0; i < events.length; i++ ) {
        	
        	Display display = ( (WindowManager)getSystemService( Context.WINDOW_SERVICE ) ).getDefaultDisplay();
        	
        	Button event = new Button( this );
        	event.setBackgroundColor( EVENT_COLOR );
        	event.setText( events[i].name );
        	//event.setOnClickListener( events[i].listener );
        	
        	int width = display.getWidth() - HOUR_LABEL_WIDTH;
        	int xPos = HOUR_LABEL_WIDTH;
        	
        	if( events[i].concurrentEvents != null ) {
        		
        		width /= events[i].concurrentEvents.size();
        		xPos += events[i].concurrentPos * width;
        	}
        	
        	components.addView( event, new AbsoluteLayout.LayoutParams( width,
        																(int)( events[i].length * HOUR_LABEL_HEIGHT ),
        																xPos,
        																(int)( events[i].startTime * HOUR_LABEL_HEIGHT ) ) );
        	
        	//	Draw a boarder around the event.
        	TextView top = new TextView( this );
        	TextView bottom = new TextView( this );
        	TextView left = new TextView( this );
        	TextView right = new TextView( this );
        	
        	top.setBackgroundColor( Color.BLACK );
        	bottom.setBackgroundColor( Color.BLACK );
        	left.setBackgroundColor( Color.BLACK );
        	right.setBackgroundColor( Color.BLACK );
        	
        	components.addView( top, new AbsoluteLayout.LayoutParams( width,
        															  1,
        															  xPos,
        															  (int)( events[i].startTime * HOUR_LABEL_HEIGHT ) ) );
        	components.addView( bottom, new AbsoluteLayout.LayoutParams( width,
        																 1,
        																 xPos,
        																 (int)( ( events[i].startTime + events[i].length ) * HOUR_LABEL_HEIGHT ) ) );
        	components.addView( left, new AbsoluteLayout.LayoutParams( 1,
        															   (int)( events[i].length * HOUR_LABEL_HEIGHT ),
        															   xPos,
        															   (int)( events[i].startTime * HOUR_LABEL_HEIGHT ) ) );
        	components.addView( right, new AbsoluteLayout.LayoutParams( 1,
        																(int)( events[i].length * HOUR_LABEL_HEIGHT ),
        																xPos + width,
        																(int)( events[i].startTime * HOUR_LABEL_HEIGHT ) ) );
        }
                
        scroll.addView( components );
        setContentView( scroll );
        
    }
    
    private class Event {
    	
    	public CharSequence name = "";
    	public double startTime = 0.0;
    	public double length = 0.0;
    	public Vector<Event> concurrentEvents = null; 
    	public int concurrentPos = 0;
    	public View.OnClickListener listener = null;
    	
    	public Event( CharSequence name, double startTime, double length ) {
    		
    		this.name = name;
    		this.startTime = startTime;
    		this.length = length;
    	}
    }
}
